//
//  SEGVisitorAccessNumberViewController.m
//  ExcellentHome
//
//  Created by 乔杨 on 2020/6/4.
//

#import "SEGVisitorAccessNumberViewController.h"

#import "SEGVisitorAccessNumberViewCell.h"

#import "SEGVisitorAccessNumberViewModel.h"

@interface SEGVisitorAccessNumberViewController ()<UITableViewDelegate, UITableViewDataSource>
/// 信息列表视图
@property (nonatomic, strong) UITableView *tableView;

/// viewModel
@property (nonatomic, strong) SEGVisitorAccessNumberViewModel *viewModel;

@end

@implementation SEGVisitorAccessNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"可用次数";
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];

    @weakify(self);
    [self.viewModel.command.executing subscribeNext:^(NSNumber * _Nullable x) {
        if (x.boolValue) {
            [SVProgressHUD show];
        } else {
            [SVProgressHUD dismiss];
        }
    }];
    [[self.viewModel.command execute:nil] subscribeNext:^(NSMutableArray *data) {
        @strongify(self);
        if (data.count == 0) {
            [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.view}];
        } else {
            [self.tableView reloadData];
        }
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedFailureReason];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.accessNumberArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGVisitorAccessNumberViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVisitorAccessNumberViewCellIdentifier forIndexPath:indexPath];
    NSString *number = [NSString stringWithFormat:@"%@次",self.viewModel.accessNumberArray[indexPath.row]];
    BOOL isSelected = self.viewModel.selectedIndex == indexPath.row ? YES:NO;
    [cell updateDoorCellWithNumber:number isSelected:isSelected];
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.viewModel.selectedIndex = indexPath.row;
    [tableView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedNumber:)]) {
        [self.delegate selectedNumber:self.viewModel.accessNumberArray[indexPath.row]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIEdgeInsets insets = UIEdgeInsetsZero;//屏幕等宽分割线
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:insets];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:insets];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:insets];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:insets];
    }
}
#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.rowHeight = 44.f;
        [_tableView registerClass:[SEGVisitorAccessNumberViewCell class] forCellReuseIdentifier:kVisitorAccessNumberViewCellIdentifier];
        
        UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.tableFooterView = footView;
    }
    return _tableView;
}
- (SEGVisitorAccessNumberViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGVisitorAccessNumberViewModel alloc] init];
    return _viewModel;
}

@end
